#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5, R = 25;
int n, m, aa[N], a[N], c[N], r, X[4], b[R][N];
string d[N];
int main() {
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> aa[i];
    bool st1 = 1;
    for (int i = 1; i <= m; i++) {
        cin >> c[i];
        int pp = log2(c[i]);
        if ((1 << pp) < c[i]) st1 = 0;
    }
    r = log2(n);
    if ((1 << r) < n) r++;
    for (int i = 1; i <= r; i++) cin >> d[i];
    int T;
    cin >> T;
    while (T--) {
        for (int i = 0; i < 4; i++) cin >> X[i];
        for (int i = 1; i <= n; i++) a[i] = (aa[i] ^ X[i % 4]);
        if (st1) {
            int cnt = log2(n);
            cnt = (1 << cnt);
            for (int i = 1; i <= n; i++) b[1][i] = i;
            for (int i = 1; i <= r; i++) {
                for (int j = 1; j <= cnt / 2; j++) {
                    if (d[i][j - 1] == '0') {
                        if (a[b[i][j * 2 - 1]] >= i) b[i + 1][j] = b[i][j * 2 - 1];
                        else b[i + 1][j] = b[i][j * 2];
                    } else {
                        if (a[b[i][j * 2]] >= i) b[i + 1][j] = b[i][j * 2];
                        else b[i + 1][j] = b[i][j * 2 - 1];
                    }
                }
                cnt >>= 1;
            }
            int rc = log2(c[1]) + 1;
            ll ans = b[rc][1];
            for (int i = 2; i <= m; i++) {
                int rc = log2(c[i]) + 1;
                ans = ans ^ (ll)(i * b[rc][1]);
            }
            cout << ans << endl;
            continue;
        }
    }
    return 0;
}
//2e9
